<template>
  <el-dialog :close-on-click-modal='false' title="单位" :visible.sync="dialogFormVisible" :before-close="cancel">
    <el-form ref="form" :rules="rules" :model="form" label-width="100px">
      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="客服昵称：" prop="nickName">
            <el-input v-model="form.nickName"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="绑定用户：" prop="userId">
            <el-select v-model="form.userId" placeholder="请选择">
              <el-option v-for="item in options" :key="item.userId" :label="item.nickName" :value="item.userId">
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="停用/启用：" prop="useFlag">
            <el-switch v-model="form.useFlag" :active-value="true" :inactive-value="false" active-text="启用"
              inactive-text="停用" active-color="#13ce66" inactive-color="#ff4949">
            </el-switch>
          </el-form-item>
        </el-col>

      </el-row>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="cancel">取 消</el-button>
      <el-button type="primary" @click="confirm">确 定</el-button>
    </div>
  </el-dialog>
</template>
<script>
import SystemUserManagement from '@/api/module/system-user-management'
export default {
  props: {
    showDialog: {
      type: Boolean,
      default: false
    },
    formData: {
      type: Object,
      required: false
    },
    api: {
      type: Function,
      required: true
    }
  },
  data () {
    return {
      options: [],
      dialogFormVisible: false,
      rules: {
        nickName: [
          { required: true, message: '请输入用户名', trigger: 'blur' }
        ],
        userId: [
          { required: true, message: '请输入用户名', trigger: 'blur' }
        ],
      },
      form: {
        nickName: ''
      },
    };
  },
  watch: {
    showDialog: {
      handler (v) {
        this.dialogFormVisible = v
        v && (this.form = { ...this.form, ...this.formData }) && this.queryPageList()
      }
    }
  },
  methods: {
    queryPageList () {
      SystemUserManagement.queryByCondition({ userType: "platform_user" }).then(res => {
        if (res.code === 200) {
          this.options = res.data
        } else {
          this.options = []
        }
      })
    },
    //关闭 isRef是否刷新表单
    close (isRef) {
      this.$emit('closeDialog', isRef)
      this.$refs.form.resetFields()
    },
    //取消
    cancel () {
      this.form = {
        unitName: ''
      }
      this.close()
    },
    //确认
    confirm () {
      this.$refs.form.validate((valid) => {
        if (valid) {
          //  this.api 添加编辑接口 （注意传参）
          this.api(this.form).then(res => {
            if (res.code == 200) {
              this.$message({
                type: 'success',
                message: '操作成功!'
              });
              this.close(true)
            } else {
              this.$message({
                type: 'info',
                message: res.msg || '操作失败!'
              });
            }
          })
        } else {
          return false;
        }
      });
    }
  }
};
</script>

<style lang="scss" scoped>
::v-deep .el-select {
  width: 100%;
  display: inline-block;
  position: relative;
}
</style>